<?php namespace App\Admin\Experience\Stats;

use Input;
use DB;
//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\BetweenCriteria;

use App\Admin\Orders\Orders\Meta;
use App\Admin\Orders\Orders\Repository;

trait Service {
    
    use \App\Core\Service\Service;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
        ini_set('memory_limit', '3000M');
        set_time_limit(0);
    }
	
    //主页
    public function index(){
        $monthRaw = Input::get('month');
        if(!$monthRaw){
            $monthRaw = date('Y-m');
        }
        list($year, $month) = explode('-', $monthRaw);
        $monthName = $year.'年'.str_replace('0', '', $month).'月份';
        //统计区间
        $startTime = strtotime($year.'-'.$month.'-01');

        if(str_replace('0', '', $month) < 12){
            $endTime = strtotime($year.'-'.(intval($month) + 1).'-01');
        }else{
            $endTime = strtotime((intval($year) + 1).'-01-01');
        }

        //当前月的前一个月
        $frontTime = $startTime-2678400;
        $afterTime = $endTime-2678400;

        //所有记录
        $records = $this->repository->resetQuery()->resetCriteria()->addCriterias([
            new BetweenCriteria('created_at', $startTime, $endTime, true, false),
            new FixedCriteria('type', '4')
        ])->all();

        //前一个的所有记录
        $frontrecords = $this->repository->resetQuery()->resetCriteria()->addCriterias([
            new BetweenCriteria('created_at', $frontTime, $afterTime, true, false),
            new FixedCriteria('type', '4')
        ])->all();
        //总订单数
        $userCount = count($records);
        $frontuserCount = count($frontrecords);

        //较上个月
        if(($frontuserCount == '0') && ($userCount != '0')){
            $incrMonth = '100.00%';
        }else if($frontuserCount == '0'){
            $incrMonth = '0.00%';
        }else{
            $incrMonth = (round($userCount/$frontuserCount,2)).'%';
        }

//        $days = [];
//        $timesDaily = [];
//        $timeDaily = [];
//        $endday = date('d', $endTime-3600);
//        for($i = 1;$i<=$endday;$i++){
//            //wyc-2018-7-24 修改
//            if($i<10){
//                $days[] = $year.'-'.$month.'-0'.$i;
//                $timesDaily[$year.'-'.$month.'-0'.$i] = [];
//                $timeDaily[$year.'-'.$month.'-0'.$i] = [];
//            }else{
//                $days[] = $year.'-'.$month.'-'.$i;
//                $timesDaily[$year.'-'.$month.'-'.$i] = [];
//                $timeDaily[$year.'-'.$month.'-'.$i] = [];
//            }
//
//        }

        //总订单完成数
        $useTimes = $this->repository->resetQuery()->resetCriteria()->addCriterias([
            new BetweenCriteria('created_at', $startTime, $endTime, true, false),
            new FixedCriteria('status', '3'),
            new FixedCriteria('type', '4')
        ])->count();
        //前一个的总订单完成数
        $frontuseTimes = $this->repository->resetQuery()->resetCriteria()->addCriterias([
            new BetweenCriteria('created_at', $frontTime, $afterTime, true, false),
            new FixedCriteria('status', '3'),
            new FixedCriteria('type', '4')
        ])->count();
        //较上个月
        if($frontuseTimes == '0' && $useTimes != '0'){
            $incrMonth1 = '100.00%';
        }else if($frontuseTimes == '0'){
            $incrMonth1 = '0.00%';
        }else{
            $incrMonth1 = (round($useTimes/$frontuseTimes,2)).'%';
        }

        //总支付完成数
        $payTimes = $this->repository->resetQuery()->resetCriteria()->addCriterias([
            new BetweenCriteria('created_at', $startTime, $endTime, true, false),
            new FixedCriteria('status', '2'),
            new FixedCriteria('type', '4')
        ])->count();

        //总待支付
        $waitfor = DB::table('orders')
                    ->where('status','=','1')
                    ->where('is_delete','=','1')
                    ->where('type','=','4')
                    ->whereBetween('created_at',[$startTime,$endTime])
                    ->get();
        $useTime = count($waitfor);
        //前一个的总待支付
        $frontwaitfor = DB::table('orders')
            ->where('status','=','1')
            ->where('is_delete','=','1')
            ->where('type','=','4')
            ->whereBetween('created_at',[$frontTime,$afterTime])
            ->get();
        $frontuseTime = count($frontwaitfor);
        //较上个月
        if($frontuseTime == '0' && $useTime != '0'){
            $incrMonth2 = '100.00%';
        }else if($frontuseTime == '0'){
            $incrMonth2 = '0.00%';
        }else{
            $incrMonth2 = (round($useTime/$frontuseTime,2)).'%';
        }

        //总收入
        $orders = DB::table('orders')
            ->whereBetween('created_at',[$startTime,$endTime])
            ->where('is_delete','0')
            ->where('type','4')
            ->whereIn('status',[2,3])
            ->get();
        $income = 0;//总收入
        $yddiscount = 0;//运动优惠
        $qydiscount = 0;//企业优惠
        foreach($orders as $v){
            $income += $v->amount;
            $yddiscount += $v->step_fee;//运动优惠
            $qydiscount += $v->enterprise_fee;//企业优惠

        }
        $amount = $income-$yddiscount-$qydiscount;
        //前一个月总收入
        $income1 = 0;//总收入
        $yddiscount1 = 0;//运动优惠
        $qydiscount1 = 0;//企业优惠
        $frontorders = DB::table('orders')
            ->whereBetween('created_at',[$frontTime,$afterTime])
            ->where('is_delete','0')
            ->where('type','4')
            ->whereIn('status',[2,3])
            ->get();
        foreach($frontorders as $v){
            $income1 += $v->amount;
            $yddiscount1 += $v->step_fee;//运动优惠
            $qydiscount1 += $v->enterprise_fee;//企业优惠

        }
        $amount1 = $income1-$yddiscount1-$qydiscount1;
        if($amount1 == '0' && $amount != '0'){
            $frontamount = '100.00%';
        }else if($amount1 == '0'){
            $frontamount = '0.00%';
        }else{
            $frontamount = (round($amount/$amount1,2)).'%';
        }

        //订单状态对比分析
        $timesGroupData = [];
        $num = [$useTimes,$payTimes,$useTime];
        $genre = [['name'=>'订单完成'],['name'=>'支付完成'],['name'=>'待支付']];

        foreach($num as $key => $item){
            if(isset($genre[$key])){
                $timesGroupData[] = [
                    'value' => $item,
                    'name' => $genre[$key]['name']
                ];
            }
        }

        $timeGroupData = [];
        $money = [$amount,$yddiscount,$qydiscount];
        $genres = [['name'=>'总收入'],['name'=>'运动优惠'],['name'=>'企业福利优惠']];
        foreach($money as $key => $item){
            if(isset($genres[$key])){
                $timeGroupData[] = [
                    'value' => $item,
                    'name' => $genres[$key]['name']
                ];
            }
        }
        //print_r($timeGroupData);die;


        return $this->success([
            'monthName' => $monthName,
            'global' => [
                'user' => [
                    'total' => $userCount,
                    'incrMonth' => $incrMonth
                ],
                'times' => [
                    'total' => $useTimes,
                    'incrMonth' => $incrMonth1
                ],
                'time' => [
                    'total' => $useTime,
                    'incrMonth' => $incrMonth2
                ],
                'amount' => [
                    'total' => $amount,
                    'incrMonth' => $frontamount
                ]
            ],
            'group' => [
                'times' => $timesGroupData,
                'time' => $timeGroupData
            ]
        ]);
    }
}